#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>

#define PATH "/dev/test_char_drv_01"
char read_buf[256] = {0};
char write_buf[256] = {0};


/*
// 获取当前进程的PID
	pid_t pid = getpid();
	printf("pid = %d\n", pid);
	
	// 获取当前进程的PPID
	pid_t ppid = getppid();
	printf("ppid = %d\n", ppid);
*/

#define TEST_CHAR_DRV_MAGIC  'k'
#define TEST_CHAR_DRV_CMD_INIT_PIPE    _IO(TEST_CHAR_DRV_MAGIC, 0x1a)
#define TEST_CHAR_DRV_CMD_INIT_PACKET    _IO(TEST_CHAR_DRV_MAGIC, 0x1b)
#define TEST_CHAR_DRV_CMD_INIT_ROUND_ROBIN_WRITE_PACKET    _IO(TEST_CHAR_DRV_MAGIC, 0x1c)
#define TEST_CHAR_DRV_CMD_INIT_ROUND_ROBIN_READ_PACKET    _IO(TEST_CHAR_DRV_MAGIC, 0x1d)


int main()
{
    int fd = 0;
	int len = 0;
	int i = 0;
	int pid = 0;
	int ret = 0;
	

	fd = open(PATH, O_RDWR);
	if (fd < 0)
	{
		printf("open file %s error!\n", PATH);
		return -1;
	}
	
	ret = ioctl(fd, TEST_CHAR_DRV_CMD_INIT_ROUND_ROBIN_WRITE_PACKET, NULL);
	if (ret < 0)
	{
		printf("ioctrl file %s error!\n", PATH);
		return -1;			
	}

	{
		unsigned long long auto_add_cnt = 0;
		int loop_cnt = 100;
	
		sleep(1);
		
		while (--loop_cnt)
		{
			for (i=0; i<sizeof(write_buf); i++)
			{
				write_buf[i] = auto_add_cnt & 0xFF;
				auto_add_cnt++;
			}
			
			len = write(fd, write_buf, sizeof(write_buf));

			printf("write len=%d auto_add_cnt=%lld\n", len, auto_add_cnt);

			sleep(1);

			for (i=0; i<sizeof(write_buf); i++)
			{
				write_buf[i] = 0x00;//(auto_add_cnt+2) & 0xFF;
				auto_add_cnt++;
			}
			
			len = write(fd, write_buf, sizeof(write_buf));

			printf("write len=%d auto_add_cnt=%lld\n", len, auto_add_cnt);

			sleep(1);
		}
		close(fd);
	}
	
    return 0;
}
